package com.ketangpai.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ketangpai.admin.dao.StuWorkDao;
import com.ketangpai.admin.dao.WorkDao;
import com.ketangpai.admin.entity.*;
import com.ketangpai.admin.entity.dto.WorkDTO;
import com.ketangpai.admin.service.*;
import com.ketangpai.admin.utils.AliOSSUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("workService")
@Transactional
public class WorkServiceImpl extends ServiceImpl<WorkDao, Work> implements WorkService {
    @Resource
    private WorkDao dao;
    @Resource
    private StuWorkDao stuWorkDao;
    @Resource
    private AliOSSUtil aliOSSUtil;

    @Resource
    private UserCourseService userCourseService;

    @Resource
    private StuWorkService stuWorkService;
    @Resource
    private UserNoticeService userNoticeService;
    @Resource
    private UserService userService;
    /**
     * 通过ID查询单条数据
     */
    @Override
    public Work queryById(Integer id) {
        return dao.selectById(id);
    }

    /**
     * 通过课程ID查询作业
     */
    @Override
    public List<WorkDTO> queryByCid(Integer id) {
        QueryWrapper<Work> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);

        List<Work> workList = dao.selectList(wrapper);

        List<WorkDTO> workDTOList = new ArrayList<>();
        for (Work work : workList) {
            WorkDTO workDTO = new WorkDTO();
            BeanUtils.copyProperties(work, workDTO);
            //未交
            Integer countNoSubmit = stuWorkDao.countStatus(work.getId(), 0) + stuWorkDao.countStatus(work.getId(), 3);
            //未批改
            Integer countNoCheck = stuWorkDao.countStatus(work.getId(), 1);
            //已批改
            Integer countCheck = stuWorkDao.countStatus(work.getId(), 2);
            workDTO.setCountCheck(countCheck);
            workDTO.setCountNoCheck(countNoCheck);
            workDTO.setCountNoSubmit(countNoSubmit);
            workDTOList.add(workDTO);
        }
        return workDTOList;
    }

    /**
     * 新增数据
     */
    @Override
    public boolean insert(Work work) {
        if (work.getWorkFile() != null) {
            String url = aliOSSUtil.upload(work.getWorkFile());
            work.setFile(url);
        }
        boolean b = dao.insert(work) > 0;
        if (work.getStatus() == 2) {//已发布
            publishToUser(work);
        }
        return b;
    }

    /**
     * 发布作业
     */
    public void publishToUser(Work work) {
        insertNotice(work);
        insertStuWork(work);
    }

    //课程的所有学生作业新增
    public void insertStuWork(Work work) {
        List<StuWork> stuWorkList = new ArrayList<>();
        List<UserCourse> list = userCourseService.queryByCid(work.getCourseId());
        for (UserCourse userCourse : list) {
            if (userCourse.getType()==0){//如果是学生
                StuWork stuWork = new StuWork();
                stuWork.setUserId(userCourse.getUserId());
                stuWork.setWorkId(work.getId());
                User user=userService.selectById(userCourse.getUserId());
                stuWork.setUsername(user.getName());
                stuWork.setContent(work.getContent());
                stuWorkList.add(stuWork);
            }
        }
        stuWorkService.saveBatch(stuWorkList);//用户作业批量新增
    }

    //用户通知新增
    public void insertNotice(Work work) {
        List<UserNotice> userNoticeList = new ArrayList<>();
        List<UserCourse> list = userCourseService.queryByCid(work.getCourseId());
        for (UserCourse userCourse : list) {
            UserNotice userNotice = new UserNotice();
            userNotice.setUserId(userCourse.getUserId());
            userNotice.setCourseId(work.getCourseId());
            userNotice.setContentId(work.getId());
            userNotice.setActivity(work.getActivity());
            userNoticeList.add(userNotice);
        }
        userNoticeService.saveBatch(userNoticeList);//用户通知批量新增
    }

    /**
     * 修改数据
     */
    @Override
    public boolean update(Work work) {
        if (work.getWorkFile() != null) {
            String url = aliOSSUtil.upload(work.getWorkFile());
            work.setFile(url);
        }

        Work work1 = queryById(work.getId());//查询修改前作业
        //如果之前未发布，现在已发布
        if (work1.getStatus() == 1 && work.getStatus() == 2) {
            publishToUser(work);
        }

        return dao.updateById(work) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    public boolean deleteById(Integer id) {
        QueryWrapper<StuWork> wrapper=new QueryWrapper<>();
        wrapper.eq("work_id",id);
        List<StuWork> list=stuWorkService.list(wrapper);
        List<Integer> ids=new ArrayList<>();
        for (StuWork stuWork : list) {
            ids.add(stuWork.getId());
        }
        stuWorkService.removeBatchByIds(ids);//删除所有学生作业

        return dao.deleteById(id) > 0;
    }

}
